package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import CommonGui.LengthFilter;
import Messages.MessageCheckExistsEmpForClockInOut;
import Messages.MessageCheckExistsEmpForClockInOutReply;
import Messages.MessageGetUserById;
import Messages.MessageGetUserByIdReply;
import Messages.MessageReturnDecisionAboutButtonEnterOrExit;
import Messages.MessageReturnDecisionAboutButtonEnterOrExitReply;
import Messages.MessageShowAllWorkHours;
import Messages.MessageShowAllWorkHoursReply;

/**
 * 
 * @author Andrey,Igor,jeka
 * Class for creation window that includes JTextField for employee ID and two buttons for choose action
 *
 */
public class PfClock_In_Out_First  extends JPanel {
	


/**
	 * 
	 */
	private static final long serialVersionUID = -2499407430186906435L;


private MainMenu mainMenu;
	
	
	private JTextField numText;
	private JButton dohButton;
	private JPanel underPanel;
	private JPanel topPanel;
	private JPanel leftUnderPanel;
	private JPanel rightUnderPanel;	
	private int flag;
	public int x;
	private int y;
	private Dimension screenSize;
	
	public  PfClock_In_Out_First(MainMenu mm)
	{
		super();
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		// x=screenSize.width;
	   //  y=screenSize.height;
		this.mainMenu=mm;
	 
	//	this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setLayout(null);
		initialize();
		
		SwingUtilities.invokeLater(new Runnable(){
		    public void run(){
		    	
				numText.grabFocus();
		    }
		});
	}
	
	private void initialize()
	{
		x=screenSize.width;
		y=screenSize.height;
		final VKBDigitsOnly vkbPanel=new VKBDigitsOnly(new Color(200,250,230));                                                                    
		vkbPanel.setBounds(4*x/8, y/5, 6*x/22, 2*y/5);
		
			
	//	add(getTimePanel());
		
        JPanel timePanel = new JPanel();
	    timePanel.setBounds(x/3, y/11, x/5, y/25);                      	
		DigitalClock dc = new DigitalClock();
		JLabel lblTime=new JLabel();
		lblTime=dc.getTimeLabel();
		timePanel.add(lblTime);
		add(timePanel); 
	
	//	topPanel=new JPanel();
	//	topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		numText = new JTextField();
		numText.setHorizontalAlignment(JTextField.RIGHT);
		numText.setFont(new Font("Tahoma", Font.PLAIN, 27));
		numText.setColumns(10);	
		numText.setBounds(x/8, y/4, x/11, y/17);                  
		numText.setForeground(Color.BLACK);
		
		AbstractDocument doc3 = (AbstractDocument) numText.getDocument();
		doc3.setDocumentFilter(new LengthFilter(doc3.getLength(), 4));
		
		JLabel name = new JLabel("מס' עובד  :", JLabel.TRAILING);
		name.setForeground(Color.BLACK);
		name.setBounds(x/8, y/4, x/4, y/17);    
 		name.setFont(new Font("Tahoma", Font.ROMAN_BASELINE, 32));
		name.setLabelFor(numText);	
		//  topPanel.add(numText);
	
	   //	add(getTopPanel());
		
		add(numText);
		add(name);		
		add(vkbPanel);
		//add(getUnderPanel());
		 
        add(getDohButton());
        add(getClockInOutButton());
		
		 
	}

	/**
	 * Method for creation JPanel timePanel with digital clock(use class DigitalClock that return label clock)
	 * @return timePanel
	 */
 /* private JPanel getTimePanel()                   
	{
		
		JPanel timePanel = new JPanel();
		DigitalClock dc = new DigitalClock();
		JLabel lblTime=new JLabel();
		lblTime=dc.getTimeLabel();
		timePanel.add(lblTime);
	    return  timePanel;
	}  
	*/
	
	/**
	 * Method for creation underPanel that consist of  UnderLeftPanel and UnderRightPanel
	 * @return underPanel
	 */
/*	private JPanel getUnderPanel()             
	{
		underPanel=new JPanel();
		underPanel.setLayout(new BoxLayout(underPanel, BoxLayout.X_AXIS));
		//underPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		underPanel.add(getUnderLeftPanel());
		underPanel.add(getUnderRightPanel());
		
		return underPanel;
	}   */
	
	/**
	 * Method for creation JPanel rightUnderPanel that includes only one button DohButton 
	 * @return rightUnderPanel
	 */
/*	private JPanel getUnderRightPanel()             
	{
		rightUnderPanel=new JPanel();
		rightUnderPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		rightUnderPanel.add(getDohButton());	
		return rightUnderPanel;
	}  */
		
	
	
	
	
	/**
	 * Method for creation JPanel leftUnderPanel that includes only one button clockInOutButton 
	 * @return leftUnderPanel
	 */
/*	private JPanel getUnderLeftPanel()             
	{
		
		leftUnderPanel=new JPanel();
		leftUnderPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		leftUnderPanel.add(getClockInOutButton());		
		return leftUnderPanel;
	}       */
	
	/**
	 * Method for creation dohButton that check if employee exist and after this
	 * show all their work hours for one months
	 * @return dohButton
	 */
	
	
	private JButton getDohButton()
	{
		Icon dohotIcon = new ImageIcon("dohot.png");
		Image img = ((ImageIcon) dohotIcon).getImage() ;  
		Image newimg = img.getScaledInstance( x/9,y/9,  java.awt.Image.SCALE_SMOOTH ) ;            
	    dohotIcon = new ImageIcon( newimg );
		   
		dohButton=new JButton("דוח שעות",dohotIcon);
	//	dohButton.setVerticalTextPosition(AbstractButton.BOTTOM);
	//	dohButton.setHorizontalTextPosition(AbstractButton.CENTER);
	
		dohButton.setFont(new Font("Tahoma", Font.ROMAN_BASELINE, 23));
		dohButton.setForeground(Color.BLACK);
		dohButton.setBounds(11*x/100, 12*y/19, 2*x/8, y/7);
		
		dohButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
                MessageCheckExistsEmpForClockInOut msgCheck = new 	MessageCheckExistsEmpForClockInOut(numText.getText());
				
				try {
					mainMenu.ms.client.sendToServer(msgCheck);
					MessageCheckExistsEmpForClockInOutReply result1 = (MessageCheckExistsEmpForClockInOutReply)mainMenu.ms.client.GetMessage();
					
					if (result1.getUserName().equals("null"))
			    	{
			    		JOptionPane.showMessageDialog(null,"משתמש לא קיים");
			    		numText.setText("");
			    		//---- 
			    		SwingUtilities.invokeLater(new Runnable(){
			    		    public void run(){
			    		    	
			    				numText.grabFocus();
			    		    }
			    		});
			    		
			    		//----------
			    	}
					else{
				 Date dat = new Date();
				 DateFormat dfhh = new SimpleDateFormat("MM");
				 String   months = dfhh.format(dat);
				 
				 MessageShowAllWorkHours msgShowHours=new MessageShowAllWorkHours(numText.getText(), months);    //send to server employeeID and number month for get work hours
							 
				 mainMenu.ms.client.sendToServer(msgShowHours);
				
					MessageShowAllWorkHoursReply result2 = (MessageShowAllWorkHoursReply) mainMenu.ms.client.GetMessage();
				
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
										
					PfDoh_work_hours empHours=new PfDoh_work_hours(mainMenu,result2.getEmployeeHours());
					mainMenu.getContentPane().add(new PanelFunctionsClockIn(mainMenu,empHours), BorderLayout.EAST);
					mainMenu.getContentPane().add(empHours, BorderLayout.CENTER);	
					mainMenu.getContentPane().validate();
					}
				}
				catch (IOException e1) {
					// TODO Auto-generated catch block
					
				                        }
								
			}
		});
		return dohButton;
		
	}
		
	/**
	 * Method for creation JPanel  topPanel that includes only one JTextField for ID employee and label
	 * for this JTextField
	 * @return
	 */
/*	private JPanel getTopPanel()                        //panel with Text field for ID employee      
	{
		topPanel=new JPanel();
		topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		numText = new JTextField();
		numText.setHorizontalAlignment(JTextField.RIGHT);
		numText.setFont(new Font("Tahoma", Font.PLAIN, 27));
		numText.setColumns(10);	
		numText.setBounds(100, 100, 100, 100);
		numText.setForeground(Color.BLACK);
		JLabel name = new JLabel("מס' עובד  :", JLabel.TRAILING);
		name.setForeground(Color.BLACK);
		name.setFont(new Font("Tahoma", Font.ROMAN_BASELINE, 32));
		name.setLabelFor(numText);	
		topPanel.add(numText);
		topPanel.add(name);
		
		return topPanel;
	}*/
	
	
	/**
	 * Method for creation clockInOutButton that check if employee exist and after this check which button he need
	 * to press(clock in or clock out) and send this decision to class PfClock_In_Out_Second
	 * @return clockInOutButton
	 */
	private JButton getClockInOutButton()
	{
		Icon okIcon = new ImageIcon("my-clock_in_out.png");
		Image img = ((ImageIcon) okIcon).getImage() ;  
		Image newimg = img.getScaledInstance( x/9,y/9,  java.awt.Image.SCALE_SMOOTH ) ;  
		okIcon = new ImageIcon( newimg );
		JButton  clockInOutButton=new JButton("החתמת שעות",okIcon);
		//clockInOutButton.setVerticalTextPosition(AbstractButton.BOTTOM);
		//clockInOutButton.setHorizontalTextPosition(AbstractButton.CENTER);
		clockInOutButton.setFont(new Font("Tahoma", Font.ROMAN_BASELINE, 23));
		clockInOutButton.setForeground(Color.BLACK);
		clockInOutButton.setBounds(51*x/100, 12*y/19, 2*x/8, y/7);
		clockInOutButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				//-----------
					
				MessageCheckExistsEmpForClockInOut msgCheck = new 	MessageCheckExistsEmpForClockInOut(numText.getText());
				
				try {
					mainMenu.ms.client.sendToServer(msgCheck);
					MessageCheckExistsEmpForClockInOutReply result = (MessageCheckExistsEmpForClockInOutReply)mainMenu.ms.client.GetMessage();  // MESSAGE_CHECK_EXISTS_EMPLOYEE_FOR_CLOCK_IN_OUT_REPLY   not exist
					if (result.getUserName().equals("null"))
			    	{
			    		JOptionPane.showMessageDialog(null,"משתמש לא קיים");
			    		numText.setText("");
			    		
			    		
			    		SwingUtilities.invokeLater(new Runnable(){
			    		    public void run(){
			    		    	
			    				numText.grabFocus();
			    		    }
			    		});
			    	}
			    	
			    	else 
			    	{
			    		
			    	    Date dat = new Date();
			    	    DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
						String   data = df.format(dat);
			    		MessageReturnDecisionAboutButtonEnterOrExit msgDecision = new MessageReturnDecisionAboutButtonEnterOrExit(numText.getText(),data);//ask the question about what button is ENABLED
			    		mainMenu.ms.client.sendToServer(msgDecision);
			    		MessageReturnDecisionAboutButtonEnterOrExitReply result1 = (MessageReturnDecisionAboutButtonEnterOrExitReply)mainMenu.ms.client.GetMessage();//return answer about the button
			    		
			    		MessageGetUserById msgUserID = new MessageGetUserById(numText.getText());  //ask the user details by ID(here we need only nameUser)
			    		mainMenu.ms.client.sendToServer(msgUserID);
			    		MessageGetUserByIdReply resultUserID =( MessageGetUserByIdReply)mainMenu.ms.client.GetMessage(); //return userDetails
			    		
			    		flag=result1.getFlagButton();
			    		mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
						mainMenu.getContentPane().add(new PfClock_In_Out_Second(mainMenu,flag,numText.getText(),resultUserID.getEmployee()), BorderLayout.CENTER);
						mainMenu.getContentPane().validate();	
			    		
			    	}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
							
										
			}
		});
    return clockInOutButton;
		
	}
}
